<?php 
/**
 * This file is part of DomAr.
 * 
 * DomAr is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomAr is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomAr.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
* @package DomAr
* @subpackage Parser
*/
class DomArClassParserRecordProperty_Relation extends DomArClassParserRecordProperty {
	
	protected $inverse;
	
	protected $inversePropertyName;
	
	protected $multi;
	
	protected $ordered;
	
	protected $childClassName;
	
	protected $multiplicity;
	
	protected $logicalName;
	
	protected $itemValidator;
	
	protected $composition;
	
	
	protected function setOrm( array &$orm ) {

		$this->composition = $orm['type'] == 'owns';

		if( $orm['multiplicity'] == 'many' or $orm['multiplicity'] == 'children' ) {
			$this->logicalName = 'children';
			$this->multiplicity = 'many';
		
		} elseif( $orm['multiplicity'] == 'parents' ) {
			$this->logicalName = 'parents';
			$this->multiplicity = 'many';

			
		} elseif( $orm['multiplicity'] == 'parent' ) {
			$this->logicalName = 'parent';
			$this->multiplicity = 'one';

		
		} else {
			$this->logicalName = 'child';
			$this->multiplicity = 'one';
		}
		
		if( $orm['itemValidator'])
			$this->itemValidator = array('method'=>$orm['itemValidator'], 'args'=>$orm['itemValidatorArgs'] );

		$this->childClassName =  $orm['className'];
		$this->ordered =  (boolean)$orm['ordered'];
		//$this->multi =  $orm['MULTI']['ARGS'];
		
		//if( $orm['MULTI'] && count($orm['MULTI']['ARGS']) == 0)
		//	throw new RuntimeException('There must be atleast one class name for multi definition in class <strong>'.$this->className.' </strong>for property <strong>'.$this->name.'</strong>');
		
		$this->inverse =  (boolean)$orm['inverse'];
		$this->inversePropertyName =  $orm['inverse'];
		

		if( !$this->inversePropertyName && ($this->multiplicity == 'many' or $this->logicalName == 'parent')  )
			throw new RuntimeException('Currently every relation must be inversed! In class <strong>'.$this->className.'</strong> property <strong>'.$this->name.'</strong> is not inversed!');
		
		if( $this->multi && $this->multiplicity == 'many' ) 
			throw new RuntimeException('Multi relationship does not support many relationships.');
		
		if( $this->multi && $this->logicalName == 'parents' )
			throw new RuntimeException('One to many multi relationships can act only as parents.');
		
		
	}
	
	/**
	* @return DomArClassParserProperty
	*/
	public function findInversedProperty() {

		if( $this->inverse === true ) {
			
			if( $this->multi ) {
				$childClass = DomArClassParser::get($this->multi[0]);
			} else {
				$childClass = DomArClassParser::get($this->childClassName);
			}
			
			if( $this->inversePropertyName != NULL) {
				$property = $childClass->recordProperties[$this->inversePropertyName];
			} else {
				$class = $this->getClass();
				foreach( $childClass->recordProperties as $property ) {
					if( $property->composition === true && $class->instance_of( $property->inversePropertyName ) === true ) {
						break;
					}
				}
				$this->inversePropertyName = $property->name;
			}
			
			if( $property === NULL )
				throw new RuntimeException('Can\'t find matching inversed property for class <strong>'.$this->className.'</strong> property <strong>'.$this->name .'</strong> inversed to <strong>'.$this->childClassName.'</strong>!');
			
			if( ($this->logicalName == 'child' or $this->logicalName == 'children') && ($property->logicalName == 'child' or $property->logicalName == 'children') )
				throw new RuntimeException('Inversed property for <strong>'.$this->className.'</strong> property <strong>'.$this->name .'</strong> inversed to <strong>'.$this->childClassName.'</strong> property <strong>'.$property->name.'</strong> must act as parent(s) manager!');
			else if( ($this->logicalName == 'parent' or $this->logicalName == 'parents') && ($property->logicalName == 'parent' or $property->logicalName == 'parents') )
				throw new RuntimeException('Inversed property for <strong>'.$this->className.'</strong> property <strong>'.$this->name .'</strong> inversed to <strong>'.$this->childClassName.'</strong> property <strong>'.$property->name.'</strong> must act as child/children manager!');
			
			return $property;
			
		} else {
			return NULL;
		}
	}
	

	public function updateHandlerClassName( DomArClassParser $class ) {
		
		$invProperty = $this->findInversedProperty();
		
		if( $this->multiplicity === 'one' && $invProperty->multiplicity == 'one' )
			$this->handlerClassName = 'DomArOneToOne';
		
		else if( $this->multiplicity === 'one' )
			$this->handlerClassName = 'DomArOneToMany';
			
		else if( $invProperty->multiplicity === 'one' )
			$this->handlerClassName = 'DomArManyToOne';
		
		else
			$this->handlerClassName = 'DomArManyToMany';
		
		class_exists($this->handlerClassName);
	}
	
	public function updateCreateHandlerCode( DomArClassParser $class ) {
	

		$code .= 'new '.$this->handlerClassName."('".$this->className."','".$this->name."'";
		if( $this->multi )
			$code .= ",array('".implode("','",$this->multi)."')";
		else
			$code .= ",'".$this->childClassName."'";
		
		$code .= ','.(!$this->composition ? 'false' : 'true');
		
		if( $this->logicalName == 'child' or $this->logicalName == 'children')
			$code .= ',true';
		else
			$code .= ',false';
			
		$invProperty = $this->findInversedProperty();
			
		if( $this->inversePropertyName ) {
			$code .= ",'".$this->inversePropertyName."'";
		} else {
			$code .= ",NULL";
		}
		
		//if( $this->ordered or $invProperty->ordered)
		//	$code .= ',true';
		//else
			//$code .= ',false';
			
		if( $this->itemValidator['method'] ) {
			$code .= ',array(\''.$this->itemValidator['method'].'\')';
		}

		$this->createHandlerCode = $code.')';

	}

}
?>